<template>
  <!-- 发票添加修改页面 -->
  <el-dialog :visible.sync="visible" :title="!dataForm.id ? '新增' : '编辑'" :close-on-click-modal="false"
             :close-on-press-escape="false">
    <el-form :model="dataForm" :rules="dataRule" ref="dataForm" @keyup.enter.native="dataFormSubmitHandle()"
             label-width="120px">
      <el-form-item prop="orderId" label="订单号">
        <ComSelectPage ref="newPageSelect" @selectPageChange="e=>dataForm.orderId=e"
                       :getUrl="{
                          url:'/api/userOrder/page',
                          method:'get',
                          params:{page: 1,limit: 10,total: 0},
                        }" :defaultValue="dataForm.orderId"
                       clearable
                       selectLabel="id"
                       selectValue="id"
                       selectLabelTwo="receiveName"
                       searchLabel="id"
                       placeholder="请输入订单编号"></ComSelectPage>
      </el-form-item>
      <el-form-item prop="invoiceType" label="发票类型">
        <el-input v-model="dataForm.invoiceType" disabled placeholder="请填写姓名或个人企业名称"></el-input>
      </el-form-item>
      <el-form-item prop="titleType" label="抬头类型">
        <ren-radio-group v-model="dataForm.titleType" dict-type="title_type"></ren-radio-group>
      </el-form-item>
      <el-form-item prop="invoiceTitle" label="发票抬头">
        <el-input v-model="dataForm.invoiceTitle" placeholder="请填写姓名或个人企业名称"></el-input>
      </el-form-item>
      <el-form-item prop="invoiceContent" label="发票内容">
        <el-input v-model="dataForm.invoiceContent" disabled placeholder="请填写内容（如：商品明细）"></el-input>
      </el-form-item>
      <el-form-item prop="taxesNo" label="纳税人识别号" v-if="dataForm.titleType !== '个人'">
        <el-input v-model="dataForm.taxesNo" placeholder="请填写纳税人识别号"></el-input>
      </el-form-item>
      <el-form-item prop="bankName" label="开户银行" v-if="dataForm.titleType !== '个人'">
        <el-input v-model="dataForm.bankName" placeholder="请填写开户银行,选填"></el-input>
      </el-form-item>
      <el-form-item prop="bankNo" label="银行账号" v-if="dataForm.titleType !== '个人'">
        <el-input v-model="dataForm.bankNo" placeholder="请填写银行账号,选填"></el-input>
      </el-form-item>
      <el-form-item prop="registerAddress" label="注册地址" v-if="dataForm.titleType !== '个人'">
        <el-input v-model="dataForm.registerAddress" placeholder="请填写注册地址,选填"></el-input>
      </el-form-item>
      <el-form-item prop="registerPhone" label="注册电话" v-if="dataForm.titleType !== '个人'">
        <el-input v-model="dataForm.registerPhone" placeholder="请填写注册电话"></el-input>
      </el-form-item>
      <el-form-item prop="userEmail" label="邮箱号码">
        <el-input v-model="dataForm.userEmail" placeholder="请填写邮箱号码"></el-input>
      </el-form-item>
      <el-form-item prop="userPhone" label="手机号码">
        <el-input v-model="dataForm.userPhone" placeholder="请填写手机号码"></el-input>
      </el-form-item>
    </el-form>
    <template slot="footer">
      <el-button @click="visible = false">取消</el-button>
      <el-button type="primary" @click="dataFormSubmitHandle()">确认</el-button>
    </template>
  </el-dialog>
</template>

<script>
import debounce from 'lodash/debounce'

export default {
  data() {
    return {
      visible: false,
      url: '',
      dialogImageUrl: '',
      dialogVisible: false,
      imagelist: {},
      dataForm: {
        id: '',                         //id
        titleType: '个人',                  //抬头类型：个人，企业
        invoiceTitle: '',               //发票抬头
        invoiceContent: '商品明细',      //发票内容
        invoiceType: '电子普通发票',      //电子普通发票类型
        taxesNo: '',                    //纳税人识别号
        bankName: '',                   //开户银行；选填
        bankNo: '',                     //银行账号；选填
        registerAddress: '',            //注册地址；选填
        registerPhone: '',              //注册电话
        userEmail: '',                  //邮箱号码
        userPhone: '',                  //手机号码
        orderId: '',                    //订单编号
        createDate: '',                 //创建时间
      }
    }
  },
  computed: {
    dataRule() {
      return {
        orderId: [
          {required: true, message: this.$t('validate.required'), trigger: 'blur'}
        ],
        titleType: [
          {required: true, message: this.$t('validate.required'), trigger: 'blur'}
        ],
        invoiceTitle: [
          {required: true, message: this.$t('validate.required'), trigger: 'blur'}
        ],
        invoiceContent: [
          {required: true, message: this.$t('validate.required'), trigger: 'blur'}
        ],
        invoiceType: [
          {required: true, message: this.$t('validate.required'), trigger: 'blur'}
        ],
        taxesNo: [
          {required: this.dataForm.titleType !== '个人', message: this.$t('validate.required'), trigger: 'blur'}
        ],
        registerPhone: [
          {required: this.dataForm.titleType !== '个人', message: this.$t('validate.required'), trigger: 'blur'},
          {pattern: /^(?:(?:\+|00)86)?1[3-9]\d{9}$/, message: '请输入正确的手机号码', trigger: 'blur'}
        ],
        userEmail: [
          {required: true, message: this.$t('validate.required'), trigger: 'blur'},
          {pattern: /^[A-Za-z0-9._%+-]+@[A-Za-z0-9.-]+\.[A-Z|a-z]{2,}$/, message: '请输入正确的邮箱号码', trigger: 'blur'}
        ],
        userPhone: [
          {required: true, message: this.$t('validate.required'), trigger: 'blur'},
          {pattern: /^(?:(?:\+|00)86)?1[3-9]\d{9}$/, message: '请输入正确的手机号码', trigger: 'blur'}
        ],
      }
    }
  },
  watch: {
    'dataForm.titleType'(newValue, oldValue){
      this.dataForm.taxesNo = ''
      this.dataForm.bankName = ''
      this.dataForm.bankNo = ''
      this.dataForm.registerAddress = ''
      this.dataForm.registerPhone = ''
    }
  },
  methods: {
    init() {
      this.visible = true
      this.$nextTick(() => {
        this.$refs['dataForm'].resetFields()
        if (this.dataForm.id) {
          this.getInfo()
        }
      })
    },
    // 获取信息
    getInfo() {
      this.$http.get(`/api/orderInvoiceInfo/${this.dataForm.id}`).then(({data: res}) => {
        if (res.code !== 0) {
          return this.$message.error(res.msg)
        }
        this.dataForm = {
          ...this.dataForm,
          ...res.data
        }
      }).catch(() => {
      })
    },
    // 表单提交
    dataFormSubmitHandle: debounce(function () {
      this.$refs['dataForm'].validate((valid) => {
        if (!valid) {
          return false
        }
        this.$http[!this.dataForm.id ? 'post' : 'put']('/api/orderInvoiceInfo', this.dataForm).then(({data: res}) => {
          if (res.code !== 0) {
            return this.$message.error(res.msg)
          }
          this.$message({
            message: this.$t('prompt.success'),
            type: 'success',
            duration: 500,
            onClose: () => {
              this.visible = false
              this.$emit('refreshDataList')
            }
          })
        }).catch(() => {
        })
      })
    }, 1000, {'leading': true, 'trailing': false}),
  }
}
</script>
<style>
.el-upload el-upload-picture-card {
  display: none;
}
</style>
